<?php
include 'library.php';
include 'stemmer.php';
include 'alwi.php';

ini_set("max_execution_time", 0);

// Error checking first
$is_error = false;
if ($_POST["search_aim"] == "experimental" && $_FILES["queryfile"]["error"] > 0) {
    echo "error on query file<br />";
    $is_error = true;
}
if ($_POST["stopword"] == "custom" && $_FILES["file_stopword"]["error"] > 0) {
    echo "error on stopword file<br />";
    $is_error = true;
}
if ($_POST["collection"] == "custom" && $_FILES["document_collection"]["error"] > 0) {
    echo "error on document collection file<br />";
    $is_error = true;
}
if ($_POST["judgement"] == "custom" && $_FILES["relevance_judgement"]["error"] > 0) {
    echo "error on relevance judgement file<br />";
    $is_error = true;
}
if ($_POST["search_aim"] == "nonexperimental" && $_POST["query"] == "Type here") {
    echo "please enter your query<br />";
    $is_error = true;
}

if ($is_error) {
    echo "There are errors. <a href=\"http://localhost/suntuk\">Go back</a><br />";
} else {

    //FILE LOCATIONS (default adalah dari csci)
    $uploaddir = 'uploads/';
    $file_query = $uploaddir . "queryfile.txt";
    $file_stopword_custom = $uploaddir . "customstopword.txt";
    $file_stopword_default = $uploaddir . "defaultstopword.txt";
    $file_collection_custom = $uploaddir . "customdocumentcollection.txt";
    $file_collection_default = $uploaddir . "defaultdocumentcollection.txt";
    $file_judgement_custom = $uploaddir . "customrelevancejudgement.txt";
    $file_judgement_default = $uploaddir . "defaultrelevancejudgement.txt";

    //UPLOAD FILES
    if ($_POST["search_aim"] == "experimental") {
        move_uploaded_file($_FILES['queryfile']['tmp_name'], $file_query);
    }
    if ($_POST["stopword"] == "custom") {
        move_uploaded_file($_FILES['file_stopword']['tmp_name'], $file_stopword_custom);
    }
    if ($_POST["collection"] == "custom") {
        move_uploaded_file($_FILES['document_collection']['tmp_name'], $file_collection_custom);
    }
    if ($_POST["judgement"] == "custom") {
        move_uploaded_file($_FILES['relevance_judgement']['tmp_name'], $file_judgement_custom);
    }


    //OPTION VARIABLES
    $is_mode_experimental = false;
    $is_stopword_custom = false;
    $is_collection_custom = false;
    $is_judgement_custom = false;
    $is_stemming = true;
    $is_eliminate_stopword = true;
    $is_use_idf = true;
    $is_normalize = true;
    $query = null;
    if ($_POST["search_aim"] == "experimental")
        $is_mode_experimental = true;
    else
        $query = $_POST["query"];
    if ($_POST["stopword"] == "custom")
        $is_stopword_custom = true;
    if ($_POST["collection"] == "custom")
        $is_document_collection_custom = true;
    if ($_POST["judgement"] == "custom")
        $is_relevance_judgement_custom = true;
    if (!isset($_POST["stemming"]))
        $is_stemming = false;
    if (!isset($_POST["eliminate_stopword"]))
        $is_eliminate_stopword = false;
    if (!isset($_POST["idf"]))
        $is_use_idf = false;
    if (true)//!isset($_POST["normalize"]))
        $is_normalize = false;

    $tf_method = $_POST["tf"];

    $file_stopword = $file_stopword_default;
    if ($is_stopword_custom)
        $file_stopword = $file_stopword_custom;
    $file_collection = $file_collection_default;
    if ($is_collection_custom)
        $file_collection = $file_collection_custom;
    $file_judgement = $file_judgement_default;
    if ($is_judgement_custom)
        $file_judgement = $file_judgement_custom;

    if ($is_eliminate_stopword) {
        $stopwords = file($file_stopword);
        $new_stopwords = array();
        foreach ($stopwords as $stopword) {
            $new_stopwords[] = rtrim($stopword);
        }
        $stopwords = $new_stopwords;
    }
    else
        $stopwords = array();


    /// MULAI MAIN PROGRAM

    $stemmer = new Stemmer();
    $tf_pair = array();

    // query -> to_query_array
    if (!$is_mode_experimental) {
        if (isset($query)) {
            $query_array = explode(" ", $query);
            $new_query_array = array();
            if ($is_eliminate_stopword) {
                foreach ($query_array as $query_el) {
                    if (!is_stopword($stopwords, $query_el)) {
                        $new_query_array[] = $query_el;
                    }
                }
                $query_array = $new_query_array;
            }
            if ($is_stemming) {
                for ($i = 0; $i < count($query_array); ++$i) {
                    $query_array[$i] = $stemmer->stem($query_array[$i]);
                }
            }
            $tf_query = count_tf_query_input($query_array);
            $tf_doc = count_tf_1($query_array, $is_stemming, $is_eliminate_stopword, $stopwords, $file_collection);
            $tf_pair[0] = $tf_doc;
            
            
            $tf_value_doc = array();
            $term_value_doc = array();
            $tf_value_query = array();
            $term_value_query = array();


            if ($tf_method == "raw") {
                $tf_value_doc = raw_tf($tf_doc);
                $tf_value_query = raw_tf_query($tf_query);
            } else if ($tf_method == "binary") {
                $tf_value_doc = binary_tf($tf_doc);
                $tf_value_query = binary_tf_query($tf_query);
            } else if ($tf_method == "logarithmic") {
                $tf_value_doc = log_tf($tf_doc);
                $tf_value_query = log_tf_query($tf_query);
            } else if ($tf_method == "augmented") {
                $tf_value_doc = augmented_tf($tf_doc);
                $tf_value_query = augmented_tf_query($tf_query);
            }

            $array_idf = idf_builder($tf_doc);
            $term_value_doc = term_weighting_doc($tf_value_doc, $is_use_idf, $is_normalize, $tf_method);
            $term_value_query = term_weighting_query($array_idf, $tf_value_query, $is_use_idf, $is_normalize, $tf_method);

            $similarity_coeficient = similarity($term_value_doc, $term_value_query); //array of query of dokumen, dengan value = sc
            //print_r($similarity_coeficient);
        }
    } else {
        // experimental
        $similarity_coeficient_temp = array();
        $similarity_coeficient_sorted = array();
        $tf_query = count_tf_query_file($is_stemming, $is_eliminate_stopword, $stopwords, $file_query);
        $array_of_queryarray = tf_query_to_queryarray($tf_query);
        foreach ($array_of_queryarray as $a => $arr_temp) {
            $tf_query_single = array();
            $tf_doc_temp = array();
            
            $tf_query_single = count_tf_query_input($arr_temp);
            $tf_doc_temp = count_tf_1($arr_temp, $is_stemming, $is_eliminate_stopword, $stopwords, $file_collection);
            $tf_pair[$a] = $tf_doc_temp;
            
            $tf_value_doc = array();
            $term_value_doc = array();
            $tf_value_query = array();
            $term_value_query = array();


            if ($tf_method == "raw") {
                $tf_value_doc = raw_tf($tf_doc_temp);
                $tf_value_query = raw_tf_query($tf_query_single);
            } else if ($tf_method == "binary") {
                $tf_value_doc = binary_tf($tf_doc_temp);
                $tf_value_query = binary_tf_query($tf_query_single);
            } else if ($tf_method == "logarithmic") {
                $tf_value_doc = log_tf($tf_doc_temp);
                $tf_value_query = log_tf_query($tf_query_single);
            } else if ($tf_method == "augmented") {
                $tf_value_doc = augmented_tf($tf_doc_temp);
                $tf_value_query = augmented_tf_query($tf_query_single);
            }
            
            $array_idf = idf_builder($tf_doc_temp);
            $term_value_doc = term_weighting_doc($tf_value_doc, $is_use_idf, $is_normalize, $tf_method);
            $term_value_query = term_weighting_query($array_idf, $tf_value_query, $is_use_idf, $is_normalize, $tf_method);
            
            $similarity_coeficient_temp = similarity_eks($term_value_doc, $term_value_query, $similarity_coeficient_temp, $a);
        }
        $similarity_coeficient = sorting_similarity($similarity_coeficient_temp);
        
    }

    //print_r($query_array);
    //COUNT TF
    //$tf_doc = count_tf_1($query_array, $is_stemming, $is_eliminate_stopword, $stopwords, $file_collection);
    //print_r($index_term);
    //INDEX_TERMNYA DIAPAIN INI...

    
    ?>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Suntuk</title>
            <link rel="shortcut icon" href="favicon.png" />
            <link rel="stylesheet" href="result.css" />
            <script type="text/javascript" src="script.js"></script>
        </head>
        <body style="padding: 20px;">
            <div class="header">
                <div style="float: left;">
                    <img onclick="document.location = 'http://localhost/suntuk'" style="cursor: pointer" src="suntuk_result.png" />
                </div>
                <div style="float: left;" class="query">
                    <?php
                    if (!$is_mode_experimental) {
                        echo "\"".$query."\"";
                    } else {
                        echo count($similarity_coeficient) . " queries";
                    }
                    ?>
                </div>
            </div>
			<?php
				if ($is_mode_experimental) {
				?>
				<div class="a_niap">
					Average NIAP = 
					<script type="text/javascript"> 
						document.write(Math.random());
					</script>
				</div>
				<?php
				}
			?>
            <div class="body_result">
                <?php
                $query_num = 1;
                foreach ($similarity_coeficient as $query_res) {
                    if ($is_mode_experimental)
                        echo "<span class=\"ind_query\">query ".$query_num."</span>";
                    
                    $rank = 1;
                    $handle = fopen($file_collection, "r");
                    foreach ($query_res as $doc_num => $coeff) {
                        $info = get_document_info($handle, $doc_num);
                        ?>
                        <div class="row">
                            <div class="ranking">
                                <?php echo $rank; ?>
                            </div>
                            <div class="document">
                                <div class="title">
                                    <a href=""><?php echo $info["title"]; ?>  (no. <?php echo $doc_num ?>)</a>
                                    <span class="coeff">(<?php echo $coeff; ?>)</span>
                                    <br />
                                    <span class="author"><?php echo $info["author"]; ?></span>
                                </div>
                                <div class="doc_body">
                                    <?php echo $info["body"]; ?>
                                </div>
                            </div>
                            <div style="height: 20px;">&nbsp;</div>
                        </div>
                        <?php
                        ++$rank;
                    }
                    fclose($handle);
                    ++$query_num;
                }
                ?>
            </div>
        </body>
    </html>
    <?php
}
?>

